package com.wondream.myframework.design_pattern.proxy.dynamic;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

@AllArgsConstructor
@Data
public class ProxyRobotHandler implements InvocationHandler {

    private Object target;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("==> ProxyRobotHandler.invoke|name: "+method.getName());
        System.out.println("==> method "+method.getName()+" is invoked.");
        String result = (String) method.invoke(target, args);
        System.out.println("==> ProxyRobotHandler.invoke|result: "+result);
        return result;
    }

    public static void main(String[] args) throws Throwable {
        Robot robot = new Robot();
        ProxyRobotHandler proxyRobotHandler = new ProxyRobotHandler(robot);
        Object[] params = new Object[1];
        params[0] = "hello";
        Object speak = proxyRobotHandler.invoke(null, Robot.class.getMethod("speak", String.class), params);
        System.out.println("==> invoke return: "+speak);
    }
}
